var prefix = jsctx + "/memberAccount";

var memberAccount = (function () {
    function init() {
        setTimeout(function () {//防止先于-读取缓存执行
            load();
        },10)
    };

    $('#exampleTable').on('load-success.bs.table', function (e, data) {
        if (data.total && !data.rows.length) {
            $('#exampleTable').bootstrapTable('selectPage').bootstrapTable('refresh');
        }
    });

    function load() {
        $('#exampleTable')
            .bootstrapTable(
                {
                    method: 'get',
                    url: prefix + "/listMemberAccount",
                    iconSize: 'outline',
                    toolbar: '#exampleToolbar',
                    striped: false,
                    dataType: "json",
                    pagination: true,
                    singleSelect: false,
                    pageSize: 20,
                    pageList: [20],
                    pageNumber: 1,
                    sidePagination: "server",
                    cache: false,
                    queryParams: function (params) {
                        return {
                            limit: params.limit,
                            offset: (params.offset / params.limit) + 1,
                            nickname: $("#nickname").val().trim(),
                            mobile: $("#mobile").val().trim()
                            // memberType: $("#memberType").val().trim()
                        };
                    },
                    columns: [
                        {
                            title: '#', field: '', width: '50', align: 'left',
                            formatter: function (value, row, index) {
                                return index + 1;
                            }
                        },

                        {field: 'nickname', title: '会员名', width: '100', align: 'left'},
                        {
                            field: 'memberType', title: '会员类型',
                            formatter: function (value, row, index) {
                                if (value == '0') {
                                    return '<span class="label label-success">个人</span>'
                                } else if (value == '1') {
                                    return '<span class="label label-info">企业</span>'
                                } else if (value == '2') {
                                    return '<span class="label label-warning">互联互通用户</span>'
                                }
                            }
                        },
                        {field: 'mobile', title: '手机号', width: '100', align: 'left'},
                        {field: 'account', title: '会员余额', width: '100', align: 'right'},
                        {field: 'giveAccount', title: '会员赠送余额', width: '100', align: 'right'},
                        {field: 'enterpriseAccount', title: '企业分配余额', width: '100', align: 'right'},
                        {field: 'allTransferAmount', title: '企业分配时转入的总金额', width: '100', align: 'right'},
                        {
                            title: '操作',
                            field: 'roleId',
                            width: '200', align: 'center',
                            formatter: function (value, row, index) {
                                var a = '<a class="btn btn-primary btn-sm ' + has_edit + '" href="#" title="余额变动记录" onclick="memberAccount.memberAccountRecord(\''
                                    + row.memberId
                                    + '\')">余额变动记录</a> ';
                                var b = '<a class="btn btn-danger btn-sm ' + has_edit + '" href="#" title="充值" onclick="memberAccount.recharge(\''
                                    + row.memberId
                                    + '\')">充值</a> ';

                                return a + b;
                            }
                        }
                    ]
                });
    }

    function reLoad() {
        $('#exampleTable').bootstrapTable('refresh');
    }

    function goBack() {
        location.href = prefix + "/index";
    }

    function memberAccountRecord(memberId) {
        location.href = prefix + "/memberAccountRecord/" + memberId;
    }

    function recharge(memberId) {
        location.href = prefix + "/recharge/" + memberId;
    }

    return {
        init: init,
        goBack: goBack,
        reLoad: reLoad,
        memberAccountRecord: memberAccountRecord,
        recharge: recharge
    };
})();


var memberRecharge = (function () {
    function init() {
        validateRule();
    };

    /*------  验证码  -----*/
    var num1 = 60;
    var ajaxReady = true;

    function getAuthCode() {
        if (ajaxReady) {
            $.ajax({
                url: prefix + "/getMemberRechargeAuthCode",
                type: "POST",
                data: $('#frmMemberRecharge').serialize(),
                beforeSend: function () {
                    ajaxReady = false;
                },
                success: function (data) {
                    if (data.status == 200) {
                        time();

                        function time() {
                            if (num1 == 0) {
                                $('#authCode').attr("disabled", "disabled")
                                $('#sendVerificationCode').text("获取验证码");
                                $('#sendVerificationCode').attr("onclick", 'memberRecharge.getAuthCode()');
                                ajaxReady = true;
                                num1 = 60;
                            } else {
                                $('#authCode').removeAttr('disabled')
                                $('#sendVerificationCode').html(num1 + "秒后重试");
                                $('#sendVerificationCode').removeAttr('onclick');
                                num1--;
                                setTimeout(function () {
                                    time()
                                }, 1000)
                            }
                        }
                    } else {
                        parent.layer.alert(data.message);
                        $('#authCode').removeAttr('disabled')
                        $('#sendVerificationCode').text("获取验证码");
                        $('#sendVerificationCode').attr("onclick", 'memberRecharge.getAuthCode()');
                        ajaxReady = true;
                        num1 = 60;
                    }

                }
            })


        }
    };



    var isajaxsend = true;
    function save() {
        if (isajaxsend) {
            $.ajax({
                cache: true,
                type: "POST",
                url: prefix + "/saveRecharge",
                data: $('#frmMemberRecharge').serialize(),
                async: false,
                beforeSend: function () {
                    isajaxsend = false
                },
                error: function (request) {
                    isajaxsend = true
                    parent.layer.alert("Connection error");
                },
                success: function (data) {
                    if (data.status == 200) {
                        isajaxsend = false
                        parent.layer.msg("操作成功");
                        javascript:history.back(-1);
                    } else {
                        isajaxsend = true
                        parent.layer.alert(data.message)
                    }
                }
            });
        }
    }

    function validateRule() {
        var icon = "<i class='fa fa-times-circle'></i> ";
        $("#frmMemberRecharge").validate({
            rules: {
                amount: {
                    required: true,
                    number: true
                },
                // mobile: {
                //     required: true,
                //     minlength: 11
                // },
                authCode: {
                    required: true
                }
            },
            messages: {
                amount: {
                    required: icon + "请输入金额",
                    number: "请输入合法的数字(整数,负数,小数)"
                },
                // mobile: {
                //     required: icon + "请输入手机号码",
                //     minlength: "手机号码长度不能小于 11 个数字"
                // },
                authCode: {
                    required: icon + "请输入验证码"
                }
            },
            submitHandler: function (form) {
                save();
            }
        })
    }

    return {
        init: init,
        getAuthCode: getAuthCode
    };
})();
